package com.ruoyi.edit.controller;

import java.util.*;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.edit.domain.CourseContent;
import com.ruoyi.edit.service.ICourseContentService;
import com.ruoyi.edit.service.OssService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edit.domain.CourseTool;
import com.ruoyi.edit.service.ICourseToolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 道具表Controller
 * 
 * @author qiufeng
 * @date 2022-04-24
 */
@Controller
@RequestMapping("/system/courseTool")
public class CourseToolController extends BaseController
{
    private String prefix = "system/courseTool";

    @Autowired
    private ICourseToolService courseToolService;

    @Autowired
    private ICourseContentService courseContentService;

    @Autowired
    private OssService ossService;

    @Value("${oss.path}")
    public  String path;

    @Value("${oss.url}")
    public  String ossUrl;

    @GetMapping()
    public String courseTool()
    {
        return prefix + "/courseTool";
    }

    /**
     * 查询道具表列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseTool courseTool)
    {
        startPage();
        List<CourseTool> list = courseToolService.selectCourseToolList(courseTool);
        return getDataTable(list);
    }

    /**
     * 导出道具表列表
     */
    @Log(title = "道具表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseTool courseTool)
    {
        List<CourseTool> list = courseToolService.selectCourseToolList(courseTool);
        ExcelUtil<CourseTool> util = new ExcelUtil<CourseTool>(CourseTool.class);
        return util.exportExcel(list, "道具表数据");
    }

    /**
     * 新增道具表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存道具表
     */
    @Log(title = "道具表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseTool courseTool)
    {
        return toAjax(courseToolService.insertCourseTool(courseTool));
    }

    /**
     * 修改道具表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CourseTool courseTool = courseToolService.selectCourseToolById(id);
        mmap.put("courseTool", courseTool);
        return prefix + "/edit";
    }

    /**
     * 修改保存道具表
     */
    @Log(title = "道具表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseTool courseTool)
    {
        return toAjax(courseToolService.updateCourseTool(courseTool));
    }

    /**
     * 删除道具表
     */
    @Log(title = "道具表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(courseToolService.deleteCourseToolByIds(ids));
    }

    /**
     * 添加图片
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, String id) {
        String originalName = file.getOriginalFilename();
        String extension = originalName.substring(originalName.lastIndexOf("."));
        String fileName = "admin" + "_" + System.currentTimeMillis() + extension;
        String url = ossService.aliyunOssUpload(path + "common/" + fileName, file);
        CourseTool courseTool = courseToolService.selectCourseToolById(id);
        courseTool.setImgUrl(ossUrl + url);
        courseToolService.updateCourseTool(courseTool);
        return toAjax(1);
    }

    /**
     * 绑定道具
     */
    @PostMapping("/insertAuthToolAll")
    @ResponseBody
    public AjaxResult insertAuthToolAll(String contentId, String[] toolIds) {
        String ids = "";
        CourseContent courseContent = courseContentService.selectCourseContentById(contentId);
        for (int i = 0; i < toolIds.length; i++) {
            CourseTool courseTool = courseToolService.selectCourseToolById(toolIds[i]);
            if (Objects.nonNull(courseTool)) {
                if (StringUtils.equals(ids,"")){
                    ids = ids + courseTool.getId();
                }else {
                    ids = ids +","+ courseTool.getId();
                }
            }
        }
        if (Objects.nonNull(courseContent)){
            if (Objects.nonNull(courseContent.getTools())){

                String ids1  = courseContent.getTools()+","+ids;
                ids1 =   ids1.replace("[", "").replace("]", "").replace(" ", "");
                String[] s = Convert.toStrArray(ids1);
                List list = Arrays.asList(s);
                Set set = new HashSet(list);
                String[] rid=(String [])set.toArray(new String[0]);
                String s1 = Arrays.toString(rid);
                s1 =   s1.replace("[", "").replace("]", "").replace(" ", "");
                courseContent.setTools(s1);

            }else {
                courseContent.setTools(ids);
            }

            courseContentService.updateCourseContent(courseContent);
            return toAjax(1);
        }
        return AjaxResult.error("数据错误");

    }

    @GetMapping("/authTool")
    public String authTool(String contentId, ModelMap mmap) {
        mmap.put("contentId", contentId);
        return prefix + "/authTool";
    }

    @GetMapping("/idsToolList")
    public String getIdsToolAll(String contentId, ModelMap mmap) {
        CourseContent courseContent = courseContentService.selectCourseContentById(contentId);
        mmap.put("courseContent", courseContent);
        return prefix + "/idsToolList";
    }
    /**
     * 获取教具列表
     */
    @PostMapping("/getIdsToolAll")
    @ResponseBody
    public TableDataInfo getIdsToolAll(String toolIds) {
        startPage();
        List<CourseTool> list = courseToolService.getIdsToolAll(toolIds);
        return getDataTable(list);
    }


    @GetMapping("/toolParentList")
    public String toolParentList(String contentId,String toolId, ModelMap mmap) {
        if (Objects.nonNull(contentId)){
            CourseContent courseContent = courseContentService.selectCourseContentById(contentId);
            mmap.put("toolId", toolId);
            mmap.put("toolIds", courseContent.getTools());
            return prefix + "/toolParentList";
        }else {
            mmap.put("toolId", toolId);
            return prefix + "/courseToolParentList";
        }
    }


    /**
     * 新增子集道具表
     */
    @GetMapping("/addParent")
    public String addParent(String id, ModelMap mmap)
    {
        mmap.put("parentId", id);
        return prefix + "/addParent";
    }

}
